package com.android.newsreader;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

/**
* An activity class to allow additional News channels
* to be input
*/
public class InputChannel extends Activity {
	
	private RSSChannelProvider mDb4oHelper = null;

	/**
	 *  Called when the activity is first created. 
	 * */
	public void onCreate(Bundle savedInstanceState) 
    {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.channel_edit);
        
        dbHelper();
        initialiseButtons();
    }
	
	/**
	 *  Sets up action listeners for buttons
	 *  on the activity 
	 * */
	private void initialiseButtons(){
    	Button cancelbutton = (Button) findViewById(R.id.cancel);
        
        cancelbutton.setOnClickListener(new Button.OnClickListener() 
        {
            public void onClick(View v) 
            {
            	finish();
            }
        }); 
        	
        Button saveButton = (Button) findViewById(R.id.save);
            
        saveButton.setOnClickListener(new Button.OnClickListener() 
            {
                public void onClick(View v) 
                {
                	String newChannel = ((TextView) findViewById(R.id.channel)).getText().toString();
                	
                	
                	if (mDb4oHelper.findByName(newChannel).isEmpty()) {
                		RSSChannel newsRSSChannel = new RSSChannel(newChannel, getString(R.string.news_cat),"");
                		RSSChannel sportRSSChannel = new RSSChannel(newChannel, getString(R.string.sport_cat),"");
                		
                		mDb4oHelper.store(newsRSSChannel);
                		mDb4oHelper.store(sportRSSChannel);
                		finish();
                	} else {
                		Toast toast = Toast.makeText(getApplicationContext(), getString(R.string.error_save), Toast.LENGTH_LONG);
						toast.show();
                	}
                	
                }
            });

    }
	
	
	
	/**
     * Create RSSChannelProvider instance
     */
     private RSSChannelProvider dbHelper() {
         if (mDb4oHelper == null) {
            mDb4oHelper = RSSChannelProvider.getInstance(this);
            mDb4oHelper.db();
         }
         
         return mDb4oHelper;
     } 
	

}
